// pages/workTask/workTask.js

const app = getApp()
const lbbService = require('../service/service.js');
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight:0,
    taskData:[
    ],
    buttons: [{text: '取消'}, {text: '确定'}],
    dialogShow:false,
    deleteUseItem:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.PageShow()
  },
  PageShow(){
    var that=this
    lbbService.getWorkTaskList({},res=>{
      var data=res.data
      lbbService.getAllInnerUser({}, ress => {
        var userList=ress.data
      for (let index = 0; index < data.length; index++) {
        var personNames=that.updateShowPersons(userList,data[index].chargePersonId,data[index].inspectPersonId);
        data[index].chargePersonName=personNames[0]
        data[index].inspectPersonName=personNames[1]
        var cronMessage=that.CronToMessage(data[index].cron);
        data[index].cronMessage=cronMessage
      }
      that.setData({
        scrollHeight: app.getScrollHeight(-65),
        taskData:data,
      })
    })
    })
  },
  showDetail:function(e){
    let item = e.currentTarget.dataset.item

      wx.navigateTo({
        url: '/pages/workTaskDetail/workTaskDetail',
        success: function(res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('showDetail', { data: item })
        }
      })
 
  },
  longTouch:function(e){
    let item = e.currentTarget.dataset.item
    this.setData({
      deleteUseItem:item,
      dialogShow:true
    })
  },
  tapDialogButton:function(e){
      var value=e.detail.item.text
      var item=this.data.deleteUseItem
      if(value=="确定"){
        item.isDel=1
        let reg = /\//g;
        let nowTime = new Date();
        let nowDate = util.formatTime(nowTime).replace(reg, '-');
        item.create=nowDate
        //后台删除
        lbbService.updateTask(item,res=>{
          this.PageShow()
        })
      }
      this.setData({
        dialogShow: false,
        showOneButtonDialog: false,
        deleteUseItem:{},
    })
  },
  insertTask:function(){
    wx.navigateTo({
      url: '/pages/workTaskDetail/workTaskDetail',
    })
  },
  updateShowPersons(usersList,personIds,checkids){
    var personList=[]
    var personNames=""
    var checkPersons=""
    // lbbService.getAllInnerUser({}, res => {
      var idsList=personIds.split(",");
      for (let index = 0; index < idsList.length; index++) {
      for (let j = 0; j < usersList.length; j++) {
          if(usersList[j].uid==idsList[index]){
            if(idsList.length-1==index){
              personNames=personNames+usersList[j].userName
            }
            else {
              personNames=personNames+usersList[j].userName+","
            }
          }
        }
      
      }
      var checkidsList=checkids.split(",");
      for (let index = 0; index < checkidsList.length; index++) {
        for (let j = 0; j < usersList.length; j++) {
            if(usersList[j].uid==checkidsList[index]){
              if(checkidsList.length-1==index){
                checkPersons=checkPersons+usersList[j].userName
              }
              else {
                checkPersons=checkPersons+usersList[j].userName+","
              }
            }
          }
        
        }
        personList.push(personNames)
        personList.push(checkPersons)
      return personList
  // })
  },
  CronToMessage:function(cron){
    var crons=cron.split(" ");
    var min=crons[1];
    var hour=crons[2];
    var day=crons[3];
    var month=crons[4];
    var week=crons[5];
    var year=crons[6];
    if(year){
      return "一次性    "+year+"年"+month+"月"+day+"日"+hour+"时"+min+"分"
    
    }
    else if(day=='*'){
      return "每天    "+hour+"点"+min+"分"
    }
    else if(week>0&&week<8){
          var content=""
        switch(week) {
          case "1":
            content="每周    星期天";
            break;
          case "2":
            content= "每周    星期一";
            break;
             case "3":
              content= "每周    星期二";
              break;
             case "4":
              content= "每周    星期三";
              break;
             case "5":
              content= "每周    星期四";
              break;
             case "6":
              content= "每周    星期五";
              break;
             case "7":
              content= "每周    星期六";
              break;
          default:
            content= "每周    星期天";
             break;
        }
      return content;
    
    }
    else if(month=='*'&&!year){
       return "每月    "+day+"日"+hour+"点"+min+"分";
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.PageShow()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})